package com.wanqing.community.filter;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanqing.community.bean.UserBean;
import com.wanqing.community.util.KeyConstant;
import com.wanqing.community.util.StringTools;

/**
 * 验证是否登录或Session是否失效
 * All right reserved.
 * @version 
 * Company 
 */
public class CheckLoginFilter implements javax.servlet.Filter{

	private static HashMap<String, String> noPrivilegeUrl = new HashMap<String, String>();
	
	static{
		noPrivilegeUrl.put("/login.jsp", null);
		noPrivilegeUrl.put("/userLoginController.do", null);
		noPrivilegeUrl.put("/common/session.jsp", null);
		noPrivilegeUrl.put("/predictWarn.do", null);
		noPrivilegeUrl.put("/loginLimit.do", null);
		noPrivilegeUrl.put("/index.jsp", null);
		noPrivilegeUrl.put("/pages/errors.jsp", null);
		noPrivilegeUrl.put("/map.do", null);
	}	
	public void destroy() {
		
	}	
	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {		
		HttpServletRequest request = (HttpServletRequest)req;
		HttpServletResponse response = (HttpServletResponse)res;
		String url = request.getServletPath();	
		String path = request.getContextPath();	
//		System.out.println("登录成功");

		
		if(noPrivilegeUrl.containsKey(url)){
			chain.doFilter(req, res);
			return;
		}else{		
			UserBean user = (UserBean) request.getSession().getAttribute(KeyConstant.USER_SESSION);
			if(null==user || StringTools.isEmptyOrNull(user.getId())){
				response.sendRedirect(path+"/common/session.jsp");
			}else{
				chain.doFilter(req, res);
				return;
			}
		}		
	}	
	public void init(FilterConfig filterConfig) throws ServletException {
		
	}

}
